﻿using System;
using System.Collections.Generic;
using System.Text;
using Estimate.Common.DBFieldTypes;

namespace Estimate.Common.Entity
{
    /// <summary>
    /// 预报实况对比结果缓存数据集
    /// </summary>
    public class ForeRealCompareEntity
    {
        /// <summary>
        /// 预报日期
        /// </summary>
        public DateTime ForeDate;

        /// <summary>
        /// 以span为索引值，SpanForeReal为值的字典
        /// </summary>
        private Dictionary<ForeSpanType, SpanForeReal> SpanCompare;

        public ForeRealCompareEntity(DateTime foreDate)
        {
            SpanCompare = new Dictionary<ForeSpanType, SpanForeReal>();
            ForeDate = foreDate;
        }

        private void setSpanForeReal(ForeSpanType span, SpanForeReal spanForeReal)
        {
            SpanCompare[span] = spanForeReal;
        }

        public void SetSpanForeReal(ForeSpanType span, ForeFactorType factor, float fore, float real)
        {
            if( ! SpanCompare.ContainsKey(span))
            {
                SpanCompare[span] = new SpanForeReal();
            }
            SpanForeReal spanForeReal = SpanCompare[span];

            if( ! spanForeReal.ContainsKey(factor))
            {
                spanForeReal[factor] = new ForeReal(fore,real);
            }
            else
            {
                 spanForeReal[factor].Fore = fore;
                 spanForeReal[factor].Real = real;
            }
        }

        public override string ToString()
        {
            List<string> result = new List<string>();
            result.Add(ForeDate.ToString("yyyy-MM-dd"));

            foreach (KeyValuePair<ForeSpanType, SpanForeReal> pair in SpanCompare)
            {
                result.Add(string.Format("{0}*{1}", pair.Key.ToString(), pair.Value.ToString()));
            }

            return string.Join("#", result.ToArray());
        }

        public static ForeRealCompareEntity FromString(string value)
        {
            
            string[] vs = value.Split(new char[] { '#' });
            ForeRealCompareEntity result = new ForeRealCompareEntity(DateTime.Parse(vs[0]));
            for (int i = 1; i < vs.Length; i++)
            {
                string[] ss = vs[i].Split(new char[] { '*' });
                ForeSpanType span = (ForeSpanType)Enum.Parse(typeof(ForeSpanType), ss[0]);
                SpanForeReal spanForeReal = SpanForeReal.FromString(ss[1]);
                result.setSpanForeReal(span, spanForeReal);
            }
            return result;
        }
    }

    internal class SpanForeReal : Dictionary<ForeFactorType, ForeReal>
    {
        public override string ToString()
        {
            List<string> result = new List<string>();
            foreach(KeyValuePair<ForeFactorType,ForeReal> pair in this)
            {
                result.Add(string.Format("{0}:{1}", pair.Key.ToString(), pair.Value.ToString()));
            }
            return string.Join("^", result.ToArray());
        }

        public static SpanForeReal FromString(string value)
        {
            SpanForeReal result = new SpanForeReal();
            char[] sep1 = new char[] { '^' };
            char[] sep2 = new char[] { ':' };
            string[] vs = value.Split(sep1);
            foreach (string s in vs)
            {
                string[] ss = s.Split(sep2);
                ForeFactorType factor = (ForeFactorType)Enum.Parse(typeof(ForeFactorType), ss[0]);
                ForeReal forereal = ForeReal.FromString(ss[1]);
                result.Add(factor, forereal);
            }
            return result;
        }
    }


    /// <summary>
    /// 预报实况对比组
    /// </summary>
    internal class ForeReal
    {
        public float Fore;

        public float Real;


        public ForeReal( float fore, float real)
        {
            Fore = fore;
            Real = real;
        }

        public override string ToString()
        {
            return string.Format("{0:0.00};{1:0.00}", Fore, Real);
        }

        public static ForeReal FromString(string value)
        {
            string[] vs = value.Split(new char[] { ';' });
            float fore = Convert.ToSingle(vs[0]);
            float real = Convert.ToSingle(vs[1]);
            return new ForeReal(fore, real);
        }
    }
}
